﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：SysCompilerWebForm.cs
           // 文件功能描述：生成web页面
           //
           // 
           // 创建标识：周玉偈20090104
           //
           // 修改标识
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
using RuleCode.ObjectTree;
using RuleCode.TemplateEngine;
using Util = RuleCode.ObjectTree.Util;
using System.Windows.Forms;
using EnvDTE;
#endregion

namespace RuleCode.compiler
{
    class SysCompilerWebForm
    {
        private static SysCompilerWebForm sysCompilerWebForm;
        private static readonly string formPath = RuleCode.ObjectTree.Util.GetWebFormsPath + "\\";// Util.OTPath + "\\Module\\WebForms\\";
        //private static readonly string fromPath = Util.OTPath + "\\Templates\\RuleCode.Forms.单表标准模块\\";
        private static readonly string xmlPath = Util.OTPath + "\\Data\\QueryXml\\";
        private FormProperty formProperty;
        private readonly StringBuilder designerMember = new StringBuilder();
        private List<string> DeclarationList = new List<string>();
        Dictionary<string, object> values;
        DictClass dict;

        TemplateManager template;
        StreamWriter o;
        public static SysCompilerWebForm Instance
        {
            get
            {

                if (sysCompilerWebForm == null)
                {
                    sysCompilerWebForm = new SysCompilerWebForm();
                }
                return sysCompilerWebForm;
            }
        }
        public SysCompilerWebForm()
        {
            Init();

        }
        private void Init()
        {
            this.values = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
            dict = new DictClass();
        }
        public void compileSingleWebForm(SysTreeNode node)
        {
            values.Clear();
            string fromWebFormPath = Util.WebFormSingleTemplatePath;
            string toWebFormPath = Util.WebFormSingleOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);

            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
           // string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
            string TableName = ((WebFormSingleProperty)node.Property).Table;
            string FormName = node.Text;
            if (string.IsNullOrEmpty(TableName))
            {
                MessageBox.Show("Table Name need");
            }
            else
            {
                SysTreeNode table = dict.Tables.FindNode(TableName);
                table.ExpandAll();
                Util.SetAllPropery(table);
                Util.SetAllPropery(node);
                List<TableFieldProperty> Fields = getFieldsFromTable(table);
                values.Clear();
                //template.SetValue("ClassName", TableName + "_Form");
                //template.SetValue("TableName", TableName);
                //template.SetValue("Fields", table.Nodes[0].Nodes);
                // template.SetValue("NameSpace", NameSpace);
                values.Add("ClassName", formName);
                values.Add("TableName", TableName);
                //values.Add("Fields", table.Nodes[0].Nodes);
                values.Add("Fields", Fields);
                string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                values.Add("CustNameSpace", CustomizeNameSpace);
              
                List<WebFormFieldGroupProperty> fieldGroups = new List<WebFormFieldGroupProperty>();
                fieldGroups.Clear();
                foreach (SysTreeNode item in node.Nodes[0].Nodes)
                {
                    fieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                }
                List<WebFormButtonProperty> buttons = new List<WebFormButtonProperty>();
                buttons.Clear();
                foreach (SysTreeNode item in node.Nodes[1].Nodes)
                {
                    buttons.Add((WebFormButtonProperty)item.Property);

                }
 

                values.Add("Groups", fieldGroups);
                values.Add("Buttons", buttons);
                values.Add("NameSpace", NameSpace);
                values.Add("MenuItemId", node.Property.ID);

                string LabelText = ((WebFormProperty)node.Property).LabelText;
                values.Add("LabelText", LabelText);


                TemplateManager template;
                StreamWriter o;
                // Template template1 = Template.FromString("tem", new StreamReader(fromWebFormPath + "Form.vm").ReadToEnd());
                try
                {
                    template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "list.vm"));
                    template.SetValues(values);
                    o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_List" + ".aspx"), false,Encoding.UTF8);

                    o.Write(template.Process());
                    o.Close();
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.Message);
                }









                //template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "list.aspx.vm"));
                ////template.SetValue("NameSpace", NameSpace);

                ////template.SetValue("ClassName", TableName + "_Form");
                ////template.SetValue("TableName", TableName);
                ////template.SetValue("Fields", table.Nodes[0].Nodes);
                //template.SetValues(values);
                //o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_List" + ".aspx.cs"), false);

                //o.Write(template.Process());
                //o.Close();
                ////template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "list.aspx.designer.vm"));
                ////template.SetValue("NameSpace", NameSpace);

                ////template.SetValue("ClassName", TableName + "_Form");
                ////template.SetValue("TableName", TableName);
                ////template.SetValue("Fields", table.Nodes[0].Nodes);
                //template.SetValues(values);
                //o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_List" + ".aspx.designer.cs"), false);

                //o.Write(template.Process());
                //o.Close();



                //template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "edit.vm"));

                //template.SetValues(values);



                //o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_Edit" + ".aspx"), false);

                //o.Write(template.Process());
                //o.Close();

                //template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "edit.aspx.vm"));
                ////template.SetValue("NameSpace", NameSpace);

                ////template.SetValue("ClassName", TableName + "_Form");
                ////template.SetValue("TableName", TableName);
                ////template.SetValue("Fields", table.Nodes[0].Nodes);
                //template.SetValues(values);
                //o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_Edit" + ".aspx.cs"), false);

                //o.Write(template.Process());
                //o.Close();
                ////template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, "edit.aspx.designer.vm"));
                ////template.SetValue("NameSpace", NameSpace);
                ////template.SetValue("ClassName", TableName + "_Form");
                ////template.SetValue("TableName", TableName);
                ////template.SetValue("Fields", table.Nodes[0].Nodes);
                //template.SetValues(values);
                //o = new StreamWriter(Path.Combine(toWebFormPath, FormName + "_Edit" + ".aspx.designer.cs"), false);

                //o.Write(template.Process());
                //o.Close();
                //TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "rel.vm", "" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "list.aspx.vm", "_List" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "list.aspx.designer.vm", "_List" + ".aspx.designer.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "edit.vm", "_Edit" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "edit.aspx.vm", "_Edit" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "edit.aspx.designer.vm", "_Edit" + ".aspx.designer.cs", out template, out o);

                //将编译 好的文件添加到项目中
                if (Util_DTE.IsAddToProjectAtCompiled)
                {
                    try
                    {
                        if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                        {
                            string nodePath = string.Empty;
                            string sourceFileFullName = string.Empty;
                            Project project = null;
                            Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                            project = gStruct.Project;
                            nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormSingles);
                            if (project != null)
                            {
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName + "_List").TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName + "_Edit").TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Util.SetStatusInfo(error.Message);
                    }
                }
            }

        }

        private List<TableRelationFieldProperty> getRelationFields(string findTable, string relTable)
        {
            List<TableRelationFieldProperty> groupfields = new List<TableRelationFieldProperty>();
            SysTreeNode table = dict.Tables.FindNode(findTable);
            table.ExpandAll();
            Util.SetAllPropery(table);
            SysTreeNode tn = (SysTreeNode)table.Nodes["Relations"];
            if (tn != null)
            {
                foreach (SysTreeNode item in tn.Nodes)
                {
                    if (((TableRelationProperty)item.Property).Table == relTable)
                    {
                        foreach (SysTreeNode item1 in item.Nodes)
                        {
                            groupfields.Add((TableRelationFieldProperty)item1.Property);
                        }
                    }
                }
            }

            return groupfields;
        }
        private List<TableFieldProperty> getFieldsFromTable(string _TableName)
        {
            SysTreeNode table = dict.Tables.FindNode(_TableName);
            table.ExpandAll();
            Util.SetAllPropery(table);
            return getFieldsFromTable(table);

        }
        private List<TableFieldProperty> getFieldsFromTable(SysTreeNode table)
        {
            List<TableFieldProperty> fields = new List<TableFieldProperty>();
            List<GroupFieldProperty> groupfields = new List<GroupFieldProperty>();
            //SysTreeNode table = dict.Tables.FindNode(tablename);
            //Util.SetAllPropery(table);
            SysTreeNode tn = (SysTreeNode)table.Nodes["FieldGroups"];
            
            if (tn != null)
            {
                SysTreeNode tn1 = (SysTreeNode)tn.Nodes["OverView"]  ;
                if (tn1 == null)
                {
                    tn1 = (SysTreeNode)tn.Nodes["AutoLookup"]   ;
                }
                if (tn1 != null)
                {
                    foreach (SysTreeNode item in tn1.Nodes)
                    {
                        groupfields.Add((GroupFieldProperty)item.Property);
                    }
                }
            }
            if (groupfields.Count > 0)
            { 
                tn = (SysTreeNode)table.Nodes["Fields"];
                if (tn != null)
                {
                    foreach (GroupFieldProperty item in groupfields)
                    {
                        SysTreeNode item1 = (SysTreeNode)tn.Nodes[item.DataField];
                        fields.Add((TableFieldProperty)item1.Property);
                        //((TableFieldProperty)item1.Property).LabelText
                    }
                }
            }
            if (fields.Count < 1)
            {
                tn = (SysTreeNode)table.Nodes["Fields"];  
                if (tn != null)
                {
                    int i = 0;
                    foreach (SysTreeNode item in tn.Nodes)
                    {
                        if (((TableFieldProperty)item.Property).Name != "RECID"
                            && ((TableFieldProperty)item.Property).Name != "DATAAREAID")
                        {
                            i++;
                            if (i > 5)
                            {
                                break;
                            }
                            fields.Add((TableFieldProperty)item.Property);
                        }
                    }
                }
            }
            return fields;
        }



        public void compileMasterSlaveWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormMasterSlaveTemplatePath;
            string toWebFormPath = Util.WebFormMasterSlaveOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetAllPropery(node);
            Util.SetProerptyInSilent(node);

            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
            string MasterTableName = ((WebFormMasterSlaveProperty)node.Property).Table;
            string MasterFormName = node.Text;
            
            if (string.IsNullOrEmpty(MasterTableName))
            {
                MessageBox.Show("MasterTable Name need");
            }
            else
            {
                SysTreeNode mastertable = dict.Tables.FindNode(MasterTableName);
                mastertable.ExpandAll();
                if (node.Nodes[2].Nodes.Count < 1)
                {
                    MessageBox.Show("Slave must");
                }
                else
                {
                    SysTreeNode slave = (SysTreeNode)node.Nodes[2].Nodes[0];
                    Util.SetProerptyInSilent(slave);
                    string SlaveTableName = ((WebFormSingleProperty)slave.Property).Table;
                    string SlaveFormName = slave.Text;
                    if (string.IsNullOrEmpty(SlaveTableName))
                    {
                        MessageBox.Show("Slave Table Name need");
                    }
                    else
                    {

                        SysTreeNode slavetable = dict.Tables.FindNode(SlaveTableName);
                        // remember clear before add new
                        values.Clear();
                        values.Add("MasterClassName", MasterTableName + "");
                        values.Add("MasterTableName", MasterTableName);
                        List<TableFieldProperty> MasterTableFields = getFieldsFromTable(MasterTableName);
                        values.Add("MasterTableFields", MasterTableFields);
                        values.Add("NameSpace", NameSpace); 
                        values.Add("SlaveClassName", SlaveTableName + "");
                        values.Add("SlaveTableName", SlaveTableName);
                        List<TableFieldProperty> SlaveTableFields = getFieldsFromTable(SlaveTableName);
                        values.Add("SlaveTableFields", SlaveTableFields);
                        string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                        values.Add("CustNameSpace", CustomizeNameSpace);
                        List<WebFormFieldGroupProperty> masterFieldGroups = new List<WebFormFieldGroupProperty>();
                        masterFieldGroups.Clear();
                        foreach (SysTreeNode item in node.Nodes[0].Nodes)
                        {
                            masterFieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                        }
                        List<WebFormButtonProperty> masterButtons = new List<WebFormButtonProperty>();
                        masterButtons.Clear();
                        foreach (SysTreeNode item in node.Nodes[1].Nodes)
                        {
                            masterButtons.Add((WebFormButtonProperty)item.Property);
                        }

                        values.Add("formName", formName);
                        values.Add("MasterGroups", masterFieldGroups);
                        values.Add("MasterButtons", masterButtons);
                        values.Add("MenuItemId", node.Property.ID);
                        List<WebFormFieldGroupProperty> slaveFieldGroups = new List<WebFormFieldGroupProperty>();
                        slaveFieldGroups.Clear();
                        foreach (SysTreeNode item in slave.Nodes[0].Nodes)
                        {
                            slaveFieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                        }
                        List<WebFormButtonProperty> slaveButtons = new List<WebFormButtonProperty>();
                        slaveButtons.Clear();
                        foreach (SysTreeNode item in slave.Nodes[1].Nodes)
                        {
                            slaveButtons.Add((WebFormButtonProperty)item.Property);

                        }


                        values.Add("SlaveGroups", slaveFieldGroups);
                        values.Add("SlaveButtons", slaveButtons);

                        string LabelText = ((WebFormProperty)node.Property).LabelText;
                        values.Add("LabelText", LabelText);

                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "master.vm", "_MasterList" + ".aspx", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "master.aspx.vm", "_MasterList" + ".aspx.cs", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "master.aspx.designer.vm", "_MasterList" + ".aspx.designer.cs", out template, out o);

                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slavelist.vm", "_SlaveList" + ".aspx", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slavelist.aspx.vm", "_SlaveList" + ".aspx.cs", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slavelist.aspx.designer.vm", "_SlaveList" + ".aspx.designer.cs", out template, out o);

                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slaveedit.vm", "_SlaveEdit" + ".aspx", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slaveedit.aspx.vm", "_SlaveEdit" + ".aspx.cs", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, MasterFormName, "slaveedit.aspx.designer.vm", "_SlaveEdit" + ".aspx.designer.cs", out template, out o);


                        //将编译 好的文件添加到项目中
                        if (Util_DTE.IsAddToProjectAtCompiled)
                        {
                            try
                            {
                                if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                                {
                                    string nodePath = string.Empty;
                                    string sourceFileFullName = string.Empty;
                                    Project project = null;
                                    Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                                    project = gStruct.Project;
                                    nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormMasterSlaves);
                                    if (project != null)
                                    {
                                        sourceFileFullName = Path.Combine(toWebFormPath, MasterFormName + "_MasterList").TrimEnd("\\".ToCharArray());
                                        Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                                        sourceFileFullName = Path.Combine(toWebFormPath, MasterFormName + "_SlaveList").TrimEnd("\\".ToCharArray());
                                        Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                                        sourceFileFullName = Path.Combine(toWebFormPath, MasterFormName + "_SlaveEdit").TrimEnd("\\".ToCharArray());
                                        Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                                    }
                                }
                            }
                            catch (Exception error)
                            {
                                Util.SetStatusInfo(error.Message);
                            }
                        }
                    }
                }
            }
        }
        public void compileTreeWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormTreeTemplatePath;
            string toWebFormPath = Util.WebFormTreeOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);
            Util.SetAllPropery(node);
            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
            string TableName = ((WebFormTreeProperty)node.Property).Table;
            string FormName = node.Text;

            if (string.IsNullOrEmpty(TableName))
            {
                MessageBox.Show("Tree Table Name need");
            }
            else
            {
                string Name = ((WebFormTreeProperty)node.Property).Name;
                string ID = ((WebFormTreeProperty)node.Property).ID;
                string PID = ((WebFormTreeProperty)node.Property).PID;


                // remember clear before add new
                values.Clear();
                values.Add("ClassName", formName);
                values.Add("TableName", TableName);
                //values.Add("MasterTableFields", mastertable.Nodes[0].Nodes);
                values.Add("NameSpace", NameSpace);
                values.Add("Name", Name + "");
                values.Add("ID", ID);
                values.Add("PID", PID);
                string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                values.Add("CustNameSpace", CustomizeNameSpace);
                List<WebFormFieldGroupProperty> fieldGroups = new List<WebFormFieldGroupProperty>();
                fieldGroups.Clear();
                foreach (SysTreeNode item in node.Nodes[0].Nodes)
                {
                    fieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                }
                List<WebFormButtonProperty> buttons = new List<WebFormButtonProperty>();
                buttons.Clear();
                foreach (SysTreeNode item in node.Nodes[1].Nodes)
                {
                    buttons.Add((WebFormButtonProperty)item.Property);
                }
                values.Add("Groups", fieldGroups);
                values.Add("Buttons", buttons);

                values.Add("MenuItemId", node.Property.ID);

                string LabelText = ((WebFormProperty)node.Property).LabelText;
                values.Add("LabelText", LabelText);

                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "tree.vm", "" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "tree.aspx.vm", "" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "tree.aspx.designer.vm", "" + ".aspx.designer.cs", out template, out o);

                //将编译 好的文件添加到项目中
                if (Util_DTE.IsAddToProjectAtCompiled)
                {
                    try
                    {
                        if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                        {
                            string nodePath = string.Empty;
                            string sourceFileFullName = string.Empty;
                            Project project = null;
                            Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                            project = gStruct.Project;
                            nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormTrees);
                            if (project != null)
                            {
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName).TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Util.SetStatusInfo(error.Message);
                    }
                }
            }
        }
        public void compileParameterWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormParameterTemplatePath;
            string toWebFormPath = Util.WebFormParameterOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);
            Util.SetAllPropery(node);
            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
            string TableName = ((WebFormParameterProperty)node.Property).Table;
            string FormName = node.Text;
            if (string.IsNullOrEmpty(TableName))
            {
                MessageBox.Show("Table Name need");
            }
            else
            {
                // remember clear before add new
                values.Clear();
                values.Add("ClassName", formName );
                values.Add("TableName", TableName);
                //values.Add("MasterTableFields", mastertable.Nodes[0].Nodes);
                values.Add("NameSpace", NameSpace);
                values.Add("MenuItemId", node.Property.ID);
                List<WebFormFieldGroupProperty> fieldGroups = new List<WebFormFieldGroupProperty>();
                fieldGroups.Clear();
                foreach (SysTreeNode item in node.Nodes[0].Nodes)
                {
                    fieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                }
                List<WebFormButtonProperty> buttons = new List<WebFormButtonProperty>();
                buttons.Clear();
                foreach (SysTreeNode item in node.Nodes[1].Nodes)
                {
                    buttons.Add((WebFormButtonProperty)item.Property);

                }
                values.Add("Groups", fieldGroups);
                values.Add("Buttons", buttons);

                string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                values.Add("CustNameSpace", CustomizeNameSpace);

                string LabelText = ((WebFormProperty)node.Property).LabelText;
                values.Add("LabelText", LabelText);

                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "para.vm", "" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "para.aspx.vm", "" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "para.aspx.designer.vm", "" + ".aspx.designer.cs", out template, out o);

                //将编译 好的文件添加到项目中
                if (Util_DTE.IsAddToProjectAtCompiled)
                {
                    try
                    {
                        if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                        {
                            string nodePath = string.Empty;
                            string sourceFileFullName = string.Empty;
                            Project project = null;
                            Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                            project = gStruct.Project;
                            nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormParameters);
                            if (project != null)
                            {
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName).TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Util.SetStatusInfo(error.Message);
                    }
                }
            }

        }
        public void compileParaByUserWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormParaByUserTemplatePath;
            string toWebFormPath = Util.WebFormParaByUserOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);
            Util.SetAllPropery(node);
            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
            string TableName = ((WebFormParaByUserProperty)node.Property).Table;
            string UserField = ((WebFormParaByUserProperty)node.Property).User ;
            if (UserField == "")
            {
                UserField = "Userid";
            }
            string FormName = node.Text;
            if (string.IsNullOrEmpty(TableName))
            {
                MessageBox.Show("Table Name need");
            }
            else
            {

                // remember clear before add new
                values.Clear();
                values.Add("ClassName", formName );
                values.Add("TableName", TableName);
                //values.Add("MasterTableFields", mastertable.Nodes[0].Nodes);
                values.Add("NameSpace", NameSpace);
                values.Add("UserField", UserField);
                List<WebFormFieldGroupProperty> fieldGroups = new List<WebFormFieldGroupProperty>();
                fieldGroups.Clear();
                foreach (SysTreeNode item in node.Nodes[0].Nodes)
                {
                    fieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                }
                List<WebFormButtonProperty> buttons = new List<WebFormButtonProperty>();
                buttons.Clear();
                foreach (SysTreeNode item in node.Nodes[1].Nodes)
                {
                    buttons.Add((WebFormButtonProperty)item.Property);

                }
                values.Add("Groups", fieldGroups);
                values.Add("Buttons", buttons);
                values.Add("MenuItemId", node.Property.ID);

                string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                values.Add("CustNameSpace", CustomizeNameSpace);

                string LabelText = ((WebFormProperty)node.Property).LabelText;
                values.Add("LabelText", LabelText);


                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "pbu.vm", "" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "pbu.aspx.vm", "" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "pbu.aspx.designer.vm", "" + ".aspx.designer.cs", out template, out o);

                //将编译 好的文件添加到项目中
                if (Util_DTE.IsAddToProjectAtCompiled)
                {
                    try
                    {
                        if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                        {
                            string nodePath = string.Empty;
                            string sourceFileFullName = string.Empty;
                            Project project = null;
                            Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                            project = gStruct.Project;
                            nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormParaByUsers);
                            if (project != null)
                            {
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName).TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Util.SetStatusInfo(error.Message);
                    }
                }

            }

        }
        public void compileRelationWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormRelationTemplatePath;
            string toWebFormPath = Util.WebFormRelationOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);
            Util.SetAllPropery(node);
            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
            string MasterTableName = ((WebFormRelationProperty)node.Property).Table;
            string MiddleTableName = ((WebFormRelationProperty)node.Property).MiddleTable;
            string FormName = node.Text;

            
            
            if (string.IsNullOrEmpty(MasterTableName))
            {
                MessageBox.Show("MasterTable Name need");
            }
            else
            {
                SysTreeNode mastertable = dict.Tables.FindNode(MasterTableName);
                mastertable.ExpandAll();
                if (node.Nodes[2].Nodes.Count < 1)
                {
                    MessageBox.Show("Related Table must");
                }
                else
                {
                    SysTreeNode related = (SysTreeNode)node.Nodes[2].Nodes[0];
                    Util.SetProerptyInSilent(related);
                    string RelatedTableName = ((WebFormSingleProperty)related.Property).Table;
                    string RelatedFormName = related.Text;
                    if (string.IsNullOrEmpty(RelatedTableName))
                    {
                        MessageBox.Show("Related Table Name need");
                    }
                    else
                    {
                        SysTreeNode relatedtable = dict.Tables.FindNode(RelatedTableName);
                        // remember clear before add new
                        values.Clear();
                        values.Add("MasterClassName", MasterTableName + "");
                        values.Add("MasterTableName", MasterTableName);
                        values.Add("MasterTableFields", mastertable.Nodes[0].Nodes);
                        values.Add("NameSpace", NameSpace);
                        values.Add("RelatedClassName", RelatedTableName + "");
                        values.Add("RelatedTableName", RelatedTableName);
                        values.Add("RelatedTableFields", relatedtable.Nodes[0].Nodes);
                        List<WebFormFieldGroupProperty> masterFieldGroups = new List<WebFormFieldGroupProperty>();
                        masterFieldGroups.Clear();
                        foreach (SysTreeNode item in node.Nodes[0].Nodes)
                        {
                            masterFieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                        }
                        List<WebFormButtonProperty> masterButtons = new List<WebFormButtonProperty>();
                        masterButtons.Clear();
                        foreach (SysTreeNode item in node.Nodes[1].Nodes)
                        {
                            masterButtons.Add((WebFormButtonProperty)item.Property);
                        }


                        values.Add("MasterGroups", masterFieldGroups);
                        values.Add("MasterButtons", masterButtons);
                        values.Add("MenuItemId", node.Property.ID);
                        List<WebFormFieldGroupProperty> relatedFieldGroups = new List<WebFormFieldGroupProperty>();
                        relatedFieldGroups.Clear();
                        foreach (SysTreeNode item in related.Nodes[0].Nodes)
                        {
                            relatedFieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                        }
                        List<WebFormButtonProperty> relatedButtons = new List<WebFormButtonProperty>();
                        relatedButtons.Clear();
                        foreach (SysTreeNode item in related.Nodes[1].Nodes)
                        {
                            relatedButtons.Add((WebFormButtonProperty)item.Property);

                        }


                        values.Add("RelatedGroups", relatedFieldGroups);
                        values.Add("RelatedButtons", relatedButtons);
                        string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                        values.Add("CustNameSpace", CustomizeNameSpace);
                        values.Add("ClassName", formName);
                        values.Add("MiddleTableName", MiddleTableName);
                        List<TableRelationFieldProperty> GroupField1 = getRelationFields(MiddleTableName, MasterTableName);
                        List<TableRelationFieldProperty> GroupField2 = getRelationFields(MiddleTableName, RelatedTableName);
                        values.Add("GroupField1", GroupField1);
                        values.Add("GroupField2", GroupField2);
                        SysTreeNode detailtable = dict.Tables.FindNode(RelatedTableName);
                        detailtable.ExpandAll();
                        //Util.SetProerptyInSilent(detailtable);
                        Util.SetAllPropery(detailtable);
                        List<TableFieldProperty> listField = getFieldsFromTable(detailtable);
                        values.Add("listField", listField);

                        string LabelText = ((WebFormProperty)node.Property).LabelText;
                        values.Add("LabelText", LabelText);

                        TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "rel.vm", "" + ".aspx", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "rel.aspx.vm", "" + ".aspx.cs", out template, out o);
                        TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "rel.aspx.designer.vm", "" + ".aspx.designer.cs", out template, out o);
                       
                        //将编译 好的文件添加到项目中
                        if (Util_DTE.IsAddToProjectAtCompiled)
                        {
                            try
                            {
                                if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                                {
                                    string nodePath = string.Empty;
                                    string sourceFileFullName = string.Empty;
                                    Project project = null;
                                    Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                                    project = gStruct.Project;
                                    nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormRelations);
                                    if (project != null)
                                    {
                                        sourceFileFullName = Path.Combine(toWebFormPath, FormName).TrimEnd("\\".ToCharArray());
                                        Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                                    }
                                }
                            }
                            catch (Exception error)
                            {
                                Util.SetStatusInfo(error.Message);
                            }
                        }
                    }
                }
            }
        }

        public void compileBlankWebForm(SysTreeNode node)
        {
            string fromWebFormPath = Util.WebFormBlankTemplatePath;
            string toWebFormPath = Util.WebFormBlankOutputPath;
            string formName = node.Text;
            node.ExpandAll();
            Util.SetProerptyInSilent(node);
            Util.SetAllPropery(node);
            string NameSpace = RuleCode.ObjectTree.Util.RootWebFormNameSpace + ".Forms";
           // string TableName = ((WebFormParameterProperty)node.Property).Table;
            string FormName = node.Text;
            //if (string.IsNullOrEmpty(TableName))
            //{
            //    MessageBox.Show("Table Name need");
            //}
            //else
            //{
                // remember clear before add new
                values.Clear();
                values.Add("ClassName", formName  );
               // values.Add("TableName", TableName);
                //values.Add("MasterTableFields", mastertable.Nodes[0].Nodes);
                values.Add("NameSpace", NameSpace);
                values.Add("MenuItemId", node.Property.ID);
                //List<WebFormFieldGroupProperty> fieldGroups = new List<WebFormFieldGroupProperty>();
                //fieldGroups.Clear();
                //foreach (SysTreeNode item in node.Nodes[0].Nodes)
                //{
                //    fieldGroups.Add((WebFormFieldGroupProperty)item.Property);
                //}
                //List<WebFormButtonProperty> buttons = new List<WebFormButtonProperty>();
                //buttons.Clear();
                //foreach (SysTreeNode item in node.Nodes[1].Nodes)
                //{
                //    buttons.Add((WebFormButtonProperty)item.Property);

                //}
                //values.Add("Groups", fieldGroups);
                //values.Add("Buttons", buttons);
                string CustomizeNameSpace = RuleCode.ObjectTree.Util.RootClassNameSpace;
                values.Add("CustNameSpace", CustomizeNameSpace);

                string LabelText = ((HelpTextProperty)node.Property).LabelText;
                values.Add("LabelText", LabelText);

                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "blank.vm", "" + ".aspx", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "blank.aspx.vm", "" + ".aspx.cs", out template, out o);
                TemplateProcess(fromWebFormPath, toWebFormPath, FormName, "blank.aspx.designer.vm", "" + ".aspx.designer.cs", out template, out o);

                //将编译 好的文件添加到项目中
                if (Util_DTE.IsAddToProjectAtCompiled)
                {
                    try
                    {
                        if (Util_DTE.AddIn_ApplicationObject != null && Util_DTE.AddIn_ApplicationObject.Solution.Count > 0)
                        {
                            string nodePath = string.Empty;
                            string sourceFileFullName = string.Empty;
                            Project project = null;
                            Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm];
                            project = gStruct.Project;
                            nodePath = Path.Combine(gStruct.GeneratePath, OT.WebFormBlank);
                            if (project != null)
                            {
                                sourceFileFullName = Path.Combine(toWebFormPath, FormName).TrimEnd("\\".ToCharArray());
                                Util_DTE.AddGeneratedFileToProject_WebForm(project, nodePath, sourceFileFullName);
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        Util.SetStatusInfo(error.Message);
                    }
                
            }
        }

        private void TemplateProcess(string fromWebFormPath, string toWebFormPath, string TableName, string sourceFileName, string outputSuffix, out TemplateManager template, out StreamWriter o)
        {
            template = TemplateManager.FromFile(Path.Combine(fromWebFormPath, sourceFileName));
            template.SetValues(values);
            o = new StreamWriter(Path.Combine(toWebFormPath, TableName + outputSuffix),false,Encoding.UTF8);
            o.Write(template.Process());
            o.Close();
        }
    }
}
